package com.atguigu.gulimall.ware.listener;

import com.atguigu.common.constant.MqConstant;
import com.atguigu.common.to.mq.StockLockedTo;
import com.atguigu.gulimall.ware.entity.WmsWareOrderTaskEntity;
import com.atguigu.gulimall.ware.feign.OrderFeign;
import com.atguigu.gulimall.ware.service.WmsWareOrderTaskDetailService;
import com.atguigu.gulimall.ware.service.WmsWareOrderTaskService;
import com.atguigu.gulimall.ware.service.WmsWareSkuService;
import com.atguigu.gulimall.ware.vo.OrderVo;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;

@Component
@RabbitListener(queues = MqConstant.WARE_RELEASE_QUEUE)
public class StockReleaseListener {
    @Resource
    private WmsWareOrderTaskService wmsWareOrderTaskService;
    @Resource
    private WmsWareOrderTaskDetailService wmsWareOrderTaskDetailService;
    @Resource
    private WmsWareSkuService wmsWareSkuService;
    @Resource
    private OrderFeign orderFeign;

    @RabbitHandler
    public void handleStockLockedRelease(Message message, StockLockedTo stockLockedTo, Channel channel) throws IOException {
        System.out.println("监听到库存解锁消息-----------"+stockLockedTo.toString());
        try {
            wmsWareSkuService.unLock(stockLockedTo);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        }catch (Exception e){
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }
    }
}
